<template>
  <div class="warehouse-management-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>仓库管理</span>
          <el-button type="primary" @click="handleCreate">新建仓库</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="queryForm.warehouseName" placeholder="请输入仓库名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="warehouseName" label="仓库名称"></el-table-column>
        <el-table-column prop="location" label="位置"></el-table-column>
        <el-table-column prop="capacity" label="容量"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <el-dialog title="新建仓库" v-model="dialogVisible" width="30%">
      <el-form :model="createForm" label-width="80px">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="createForm.warehouseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="createForm.location" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="容量" prop="capacity">
          <el-input-number v-model="createForm.capacity" :min="1"></el-input-number>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const queryForm = reactive({
  warehouseName: ''
})

const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const dialogVisible = ref(false)
const createForm = reactive({
  warehouseName: '',
  location: '',
  capacity: 1
})

const handleSearch = () => {
  // 实现查询逻辑
}

const handleCreate = () => {
  dialogVisible.value = true
}

const handleSubmit = () => {
  // 实现提交新建仓库的逻辑
  dialogVisible.value = false
}

const handleEdit = (row) => {
  // 实现编辑仓库的逻辑
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定要删除该仓库吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 实现删除逻辑
    ElMessage({
      type: 'success',
      message: '删除成功!'
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  // 根据新的页大小重新获取数据
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  // 根据新的当前页重新// 根据新的当前页重新获取数据
}

// 示例数据，用于初始化表格
const exampleMaterials = [
  { warehouseName: '仓库A', location: '广东深圳', capacity: 500 },
  { warehouseName: '仓库B', location: '广东东莞', capacity: 300 },
  { warehouseName: '仓库C', location: '广东惠州', capacity: 700 },
  // ...更多示例数据
]

// 初始化表格数据
tableData.value = exampleMaterials
total.value = exampleMaterials.length

</script>
<style scoped>
.warehouse-management-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mt-20 {
  margin-top: 20px;
}
</style>
